package com.sctvcloud.renhe.ui.adapter.holder;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.gridsum.tracker.GridsumWebDissector;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.renhe.R;
import com.sctvcloud.renhe.base.BaseActivity;
import com.sctvcloud.renhe.beans.AFileItem;
import com.sctvcloud.renhe.ui.activities.AnchorHomePageActivity;
import com.sctvcloud.renhe.ui.fragment.AnchorFragment;
import com.sctvcloud.renhe.ui.utils.GlideCircleTransform;
import com.sctvcloud.renhe.ui.utils.GlideUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yuxipan on 2018-01-15.
 */

public class AuthorHolder extends BaseAnchorHolder<AFileItem> implements View.OnClickListener {
    @BindView(R.id.iv_anchor_avatar)
    protected ImageView ivPic;
    @BindView(R.id.tv_anchor_name)
    protected CustomFontTextView tvName;
    private String homePageUrl, title, anchorId;


    public AuthorHolder(Context context, View itemView) {
        super(context, itemView);
        itemView.setOnClickListener(this);
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(AFileItem aFileItem) {
        homePageUrl = aFileItem.getAnchorInfoUrl();
        title = aFileItem.getAnchorName();
        anchorId = aFileItem.getAnchorId();
        tvName.setText(TextUtils.isEmpty(aFileItem.getAnchorName()) ? "主播" : aFileItem.getAnchorName());
        if (!TextUtils.isEmpty(aFileItem.getAnchorAvatar())) {
            GlideUtil.getGlid(context, aFileItem.getAnchorAvatar()).error(R.mipmap.icon_circle_avatar)
                    .placeholder(R.mipmap.icon_circle_avatar).transform(new GlideCircleTransform(context))
                    .into(ivPic);
        } else {
            ivPic.setImageResource(R.mipmap.icon_circle_avatar);
        }
    }

    @Override
    public void onClick(View v) {

        Intent intent = new Intent(context, AnchorHomePageActivity.class);
        if (!TextUtils.isEmpty(homePageUrl) && homePageUrl.endsWith(".json")) {
            ArrayList<Pair<String, String>> pairs = new ArrayList<>();
            pairs.add(new Pair<>("页面", "首页"));
            pairs.add(new Pair<>("栏目", "主播"));
            pairs.add(new Pair<>("位置", "主播档案"));
            pairs.add(new Pair<>("行为类型", "点击"));
            GridsumWebDissector.getInstance().trackEvent(fragment, "", title, "", 200, pairs);
            intent.putExtra(BaseActivity.EX_URL, homePageUrl);
            intent.putExtra(BaseActivity.EX_TITLE, title);
            intent.putExtra(BaseActivity.EX_ID, anchorId);
            context.startActivity(intent);
        } else {
            Toast.makeText(context, "数据错误", Toast.LENGTH_SHORT).show();
        }
    }
}
